@model IEnumerable<HDLogCenter.Infrastructure.Models.AlertRuleModel>
@{
    ViewData["Title"] = "报警规则管理";
    var project = ViewBag.Project as HDLogCenter.Infrastructure.Models.AppConfigModel;
}

<div class="layui-card">
    <div class="layui-card-header">
        <h2>报警规则管理 - @project?.AppName</h2>
        <div class="layui-btn-group">
            <a href="/Project/CreateAlertRule/@project?.Id" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-add-1"></i> 新增规则
            </a>
            <a href="/Project" class="layui-btn layui-btn-sm layui-btn-primary">
                <i class="layui-icon layui-icon-return"></i> 返回项目列表
            </a>
        </div>
    </div>
    <div class="layui-card-body">
        <table id="alertRuleTable" lay-filter="alertRuleTable"></table>
    </div>
</div>

<!-- 操作列 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>

<!-- 日志级别标签 -->
<script type="text/html" id="logLevelTpl">
    {{# if(d.logLevel === 1){ }}
        <span class="layui-badge layui-bg-red">Error (1)</span>
    {{# } else if(d.logLevel === 2){ }}
        <span class="layui-badge layui-bg-orange">Warning (2)</span>
    {{# } else if(d.logLevel === 3){ }}
        <span class="layui-badge layui-bg-blue">Information (3)</span>
    {{# } else if(d.logLevel === 4){ }}
        <span class="layui-badge layui-bg-gray">Debug (4)</span>
    {{# } else { }}
        <span class="layui-badge layui-bg-gray">Unknown ({{d.logLevel}})</span>
    {{# } }}
</script>

<!-- 启用状态标签 -->
<script type="text/html" id="enabledTpl">
    {{# if(d.isEnabled === 1){ }}
        <span class="layui-badge layui-bg-green">启用</span>
    {{# } else { }}
        <span class="layui-badge layui-bg-gray">禁用</span>
    {{# } }}
</script>

<!-- 通知类型标签 -->
<script type="text/html" id="notifyTypeTpl">
    {{# if(d.notifyType === 'dingtalk'){ }}
        <span class="layui-badge layui-bg-blue">钉钉</span>
    {{# } else if(d.notifyType === 'email'){ }}
        <span class="layui-badge layui-bg-orange">邮件</span>
    {{# } else if(d.notifyType === 'sms'){ }}
        <span class="layui-badge layui-bg-red">短信</span>
    {{# } else { }}
        <span class="layui-badge layui-bg-gray">{{d.notifyType}}</span>
    {{# } }}
</script>

@section Scripts {
    <script>
        layui.use(['table', 'layer'], function(){
            var table = layui.table;
            var layer = layui.layer;
            
            // 表格渲染
            table.render({
                elem: '#alertRuleTable',
                data: @Html.Raw(Json.Serialize(Model)),
                cols: [[
                    {field: 'id', title: 'ID', width: 80, sort: true},
                    {field: 'logLevel', title: '日志级别', width: 120, templet: '#logLevelTpl'},
                    {field: 'thresholdCount', title: '阈值次数', width: 100},
                    {field: 'timeWindow', title: '时间窗口(分钟)', width: 120},
                    {field: 'notifyType', title: '通知类型', width: 100, templet: '#notifyTypeTpl'},
                    {field: 'isEnabled', title: '状态', width: 80, templet: '#enabledTpl'},
                    {field: 'createTime', title: '创建时间', width: 180, templet: function(d){
                        return d.createTime ? new Date(d.createTime).toLocaleString() : '';
                    }},
                    {title: '操作', toolbar: '#barDemo', width: 150, fixed: 'right'}
                ]],
                page: true,
                limit: 20,
                limits: [10, 20, 50, 100],
                height: 'full-200'
            });
            
            // 行工具事件
            table.on('tool(alertRuleTable)', function(obj){
                var data = obj.data;
                if(obj.event === 'edit'){
                    window.location.href = '/Project/EditAlertRule/@project?.Id/' + data.id;
                } else if(obj.event === 'delete'){
                    layer.confirm('确定要删除这条报警规则吗？', function(index){
                        $.post('/Project/DeleteAlertRule/' + data.id, function(result){
                            if(result.success){
                                obj.del();
                                layer.msg('删除成功');
                            } else {
                                layer.msg('删除失败: ' + (result.message || '未知错误'));
                            }
                        }).fail(function(){
                            layer.msg('删除失败: 网络错误');
                        });
                        layer.close(index);
                    });
                }
            });
        });
    </script>
}
